<?php
/**
 * --------------------------------------------------
 * Copyright © 2018 PHP游戏项目学习交流群：820621745 版权所有
 *
 * 功能描述：数据库类
 *
 * 创建标识：Created by Zyc On 2018/8/13 23:28
 *
 * 修改标识：
 * 修改描述：
 *---------------------------------------------------
 */

require_once __DIR__.'/Config.php';

class DataBase
{

    private $dbtype = 'mysql';              //数据库类型
    private $dbhost = '127.0.0.1';          //数据库地址
    private $dbname = 'PHPGAME';            //数据库名字
    private $dbroot = 'PHPGAME';            //数据库账号
    private $dbpawd = 'ekSKBcH22HyXDMLb';   //数据库密码
    private $dbpro = '3306';                //数据库端口
    private $table = 'PHPGAME';             //表名
    private $where = '';                    //条件
    private $pdo = '';                      //数据库句柄
    public static $db = '';                 //数据库对象


    /**
     * 连接数据库
     * @param $config [数据库配置参数]
     * @return object [返回句柄对象]
     */
    public function connect($config = [])
    {
        if ($config) {
            $this->dbhost = $config['dbhost'];
            $this->dbname = $config['dbname'];
            $this->dbroot = $config['dbroot'];
            $this->dbpawd = $config['dbpawd'];
            $this->dbpro = $config['dbpro'];
        }
        $dsn = "$this->dbtype:host=$this->dbhost;dbname=$this->dbname";
        $this->pdo = new PDO($dsn, $this->dbroot, $this->dbpawd);
    }


    /**
     * @param $table
     * @return DataBase|string
     */
    public static function Table($table)
    {
        if (empty(self::$db)) {
            self::$db = new self();
        }

        self::$db->connect();
        self::$db->table = $table;

        return self::$db;
    }

    /**
     * 条件
     * @param $where [表名]
     * @return object 返回对象
     */
    public function where($where)
    {
        $this->where = $where;
        return $this;
    }

    /**
     * 条件
     * @return array 查询单条语句
     */
    public function find()
    {
        $SQL='SELECT * FROM '.$this->table.' where '.$this->where .' limit 1';
        $relut=$this->pdo->query($SQL)->fetchAll();
        if($relut){
            return $relut[0];
        }
        return [];
    }

    /**
     * 增
     * @param array $arr
     * @return array 查询单条语句
     */
    public function insert($arr)
    {
        $filed=' (';
        $values=' (';
        foreach ($arr as $key => $value){
            $filed=isset($filed) ? $filed.','.$key : $key;
            $values=isset($values) ? $values.','.$value : $value;
        }
        $filed.=') values ';
        $values.=')';
        $sql='insert into '.$this->table.$filed.$values;
        return $this->pdo->exec($sql);
    }

    /**
     * 改
     * @param array $arr
     * @return array 查询单条语句
     */
    public function update($arr)
    {
        $values='';
        foreach ($arr as $key => $value){
            $values=!empty($values) ? $values.",$key = '$value'" : "$key = '$value'";
        }
        $sql="UPDATE $this->table set $values where $this->where";
        return $this->pdo->exec($sql);
    }

    /**
     * 修改操作
     * @return int 受影响的行数
     *
     */
    public function exec()
    {
        return $this->pdo->exec($this->where);
    }

    /**
     * @return array [返回结果数组]
     */
    public function select()
    {
        $SQL='SELECT * FROM '.$this->table.' where '.$this->where;
        return $this->pdo->query($SQL)->fetchAll();
    }
}